/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.worker.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.openislands.oi.constant.RoleType;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.constant.TaskState;
import org.openislands.oi.dao.entity.OfNodeTaskDO;
import org.openislands.oi.manager.CommonOfNodeTaskManager;
import org.openislands.oi.pojo.base.BeanBuilder;
import org.openislands.oi.pojo.dto.OfNodeTaskDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;

@Service
public class OfNodeTaskManager {
    private static final Logger log = LoggerFactory.getLogger(OfNodeTaskManager.class);

    @Resource
    private CommonOfNodeTaskManager commonOfNodeTaskManager;

    public Boolean createOfNodeTask(OfNodeTaskDTO ofNodeTaskDTO) {
        return commonOfNodeTaskManager.createOfNodeTask(ofNodeTaskDTO);
    }

    public List<OfNodeTaskDTO> queryOfNodeTask(String jobId, String taskName, Integer taskVersion, RoleType role, String nodeId) {
        return commonOfNodeTaskManager.queryOfNodeTask(jobId, taskName, taskVersion, role, nodeId);
    }

    public List<OfNodeTaskDTO> queryOfNodeTask(String jobId, String taskName, Integer taskVersion) {
        return commonOfNodeTaskManager.queryAnyOfNodeTask(BeanBuilder.create(new OfNodeTaskDTO())
                .set(OfNodeTaskDTO::setJobId, jobId)
                .set(OfNodeTaskDTO::setTaskName, taskName)
                .set(OfNodeTaskDTO::setTaskVersion, taskVersion)
        );
    }

    public List<OfNodeTaskDTO> queryConditionOfNodeTask(Consumer<LambdaQueryWrapper<OfNodeTaskDO>> conditions) {
        return commonOfNodeTaskManager.queryConditionOfNodeTask(conditions);
    }

    public Boolean updateOfNodeTask(OfNodeTaskDTO ofNodeTaskDTO) {
        return this.updateAnyOfNodeTask(ofNodeTaskDTO, OfNodeTaskDO::getJobId, OfNodeTaskDO::getTaskName, OfNodeTaskDO::getTaskVersion, OfNodeTaskDO::getRole, OfNodeTaskDO::getNodeId);
    }

    public Boolean updateOfNodeTask(String jobId, String taskName, Integer taskVersion, RoleType role, String nodeId, OfNodeTaskDTO ofNodeTaskDTO) {
        ofNodeTaskDTO.setJobId(jobId);
        ofNodeTaskDTO.setTaskName(taskName);
        ofNodeTaskDTO.setTaskVersion(taskVersion);
        ofNodeTaskDTO.setRole(role);
        ofNodeTaskDTO.setNodeId(nodeId);
        return this.updateAnyOfNodeTask(ofNodeTaskDTO, OfNodeTaskDO::getJobId, OfNodeTaskDO::getTaskName, OfNodeTaskDO::getTaskVersion, OfNodeTaskDO::getRole, OfNodeTaskDO::getNodeId);
    }

    @SafeVarargs
    public final <Res> Boolean updateAnyOfNodeTask(OfNodeTaskDTO ofNodeTaskDTO, SFunction<OfNodeTaskDO, Res>... functions) {
        return commonOfNodeTaskManager.updateAnyOfNodeTask(ofNodeTaskDTO, functions);
    }

    public Boolean deleteOfNodeTaskByJob(String jobId) {
        return commonOfNodeTaskManager.deleteOfNodeTaskByJob(jobId);
    }

    public Boolean deleteOfNodeTask(String jobId, String taskName, Integer taskVersion) {
        return commonOfNodeTaskManager.deleteOfNodeTask(jobId, taskName, taskVersion);
    }

    public MessageCodeEnum updateState(String jobId, String taskName, Integer taskVersion, RoleType role, String nodeId, TaskState toState) {
        return commonOfNodeTaskManager.updateState(jobId, taskName, taskVersion, role, nodeId, toState);
    }
}
